package org.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.mapper.UserInfoMapper;
import org.example.pojo.UserInfo;
import org.example.service.UserInfoService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Override
    public void exportCsv(HttpServletResponse response) throws IOException {
        List<UserInfo> list = list();
        response.setContentType("text/csv");
        response.setHeader("Content-Disposition", "attachment; filename=customers.csv");
        PrintWriter writer = response.getWriter();
        writer.println("客户编号,客户姓名,身份证号,联系电话,联系地址");
        for (UserInfo user : list) {
            writer.printf("%d,%s,%s,%s,%s%n",
                    user.getCustomerId(),
                    user.getName(),
                    user.getPid(),
                    user.getTelephone(),
                    user.getAddress());
        }
        writer.flush();
        writer.close();
    }

}